package javaapplication1.dao.oracleImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javaapplication1.model.PetOwner;
import javaapplication1.dao.PetOwnerDao;

public class PetOwnerDaoOracleImpl implements PetOwnerDao {

	private Connection connection = null;
	private PreparedStatement ps = null;
	private ResultSet rs = null;

	@Override
	public boolean createPetOwner(PetOwner petOwner) {
		boolean flag = false;
		try {
			String sql = "insert into T_PETOWNER values(SEQ_T_PETOWNER.nextval,?,?,?)";
			connection = OracleConnection.getConnection();
			ps = connection.prepareStatement(sql);
			ps.setString(1, petOwner.getName());
			ps.setString(2, petOwner.getPassword());
			ps.setInt(3, petOwner.getMoney());
			int line = ps.executeUpdate();
			flag = (line == 1) ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			OracleConnection.closeConn(connection, ps, rs);
		}
		return flag;
	}

	@Override
	public boolean deletePetOwner(int id) {
		boolean flag = false;
		try {
			String sql = "delete from T_PETOWNER where id = ?";
			connection = OracleConnection.getConnection();
			ps = connection.prepareStatement(sql);
			ps.setInt(1, id);
			int line = ps.executeUpdate();
			flag = (line == 1) ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			OracleConnection.closeConn(connection, ps, rs);
		}
		return flag;
	}

	@Override
	public boolean updatePetOwner(PetOwner petOwner) {
		return false;
	}
	
	@Override
	public boolean updatePetOwner(int id, int money){
		boolean flag = false;
		try {
			String sql = "update T_PETOWNER set MONEY = ? where id = ?";
			connection = OracleConnection.getConnection();
			ps = connection.prepareStatement(sql);
			ps.setInt(1, money);
			ps.setInt(2, id);
			int line = ps.executeUpdate();
			flag = (line == 1) ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			OracleConnection.closeConn(connection, ps, rs);
		}
		return flag;
	}

	@Override
	public PetOwner retrievePetOwner(int id) {
		PetOwner petOwner = null;
		try {
			String sql = "select * from T_PETOWNER where id = ?";
			connection = OracleConnection.getConnection();
			ps = connection.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				petOwner = new PetOwner();
				petOwner.setId(rs.getInt("ID"));
				petOwner.setName(rs.getString("NAME"));
				petOwner.setPassword(rs.getString("PASSWORD"));
				petOwner.setMoney(rs.getInt("MONEY"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			OracleConnection.closeConn(connection, ps, rs);
		}
		return petOwner;
	}

	@Override
	public List<PetOwner> retrievePetOwnerAll() {
		List<PetOwner> petOwnerList = new ArrayList<PetOwner>();
		try {
			String sql = "select * from T_PETOWNER";
			connection = OracleConnection.getConnection();
			ps = connection.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				PetOwner petOwner = new PetOwner();
				petOwner.setId(rs.getInt("ID"));
				petOwner.setName(rs.getString("NAME"));
				petOwner.setPassword(rs.getString("PASSWORD"));
				petOwner.setMoney(rs.getInt("MONEY"));
				petOwnerList.add(petOwner);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			OracleConnection.closeConn(connection, ps, rs);
		}
		return petOwnerList;
	}

}